#!/usr/env python

import csv
from datetime import datetime

def write_doc_comment(test_file, author, date, case_id, purpose, priority):
    test_file.write("/**\n")
    test_file.write(" * @author: %s\n" % author)
    test_file.write(" * @date: %s\n" % date.isoformat())
    test_file.write(" * @version: 0.1\n")
    test_file.write(" * @TCID: %s\n" % case_id)
    test_file.write(" * Purpose: test %s\n" % purpose)
    test_file.write(" * Priority: %s\n" % priority)
    test_file.write(" */\n")

def get_day_test(test_file, test_name, month, day, year, expected):
    test_file.write("@Test public void %s() {\n" % test_name)
    test_file.write("    Year year = new Year(%s);\n" % year)
    test_file.write("    Month month = new Month(%s, year);\n" % month)
    test_file.write("    Day day = new Day(%s, month);\n" % day)
    test_file.write("    assertEquals(%s, day.getDay());\n" % expected)
    test_file.write("}\n\n")

def set_day_test(test_file, test_name, month, day, year, expected):
    test_file.write("@Test public void %s() {\n" % test_name)
    test_file.write("    Day day = new Day(0, null);\n")
    test_file.write("    day.setDay(%s, null);\n" % day)
    test_file.write("    assertEquals(%s, day.getDay());\n" % expected)
    test_file.write("}\n\n")

def increment_test(test_file, test_name, month, day, year, expected):
    #print test_name, month, day, year, expected
    if month == 'null' or year == 'null':
        test_file.write("@Test(expected = NullPointerException.class)\n")
        test_file.write("public void %s() {\n" % test_name)
    else:
        test_file.write("@Test public void %s() {\n" % test_name)

    if year == 'null':
        test_file.write("    Year year = null;\n")
    else:
        test_file.write("    Year year = new Year(%s);\n" % year)

    if month == 'null':
        test_file.write("    Month month = null;\n")
    else:
        test_file.write("    Month month = new Month(%s, year);\n" % month)

    test_file.write("    Day day = new Day(%s, month);\n" % day)
    test_file.write("    boolean result = day.increment();\n")

    if month != 'null' and year != 'null':
        exp_result, exp_pos = expected.split('/')
        if exp_result == 'true':
            test_file.write("    assertTrue(result);\n")
        else:
            test_file.write("    assertFalse(result);\n")
        test_file.write("    assertEquals(%s, day.getDay());\n" % exp_pos)
    test_file.write("}\n\n")

with open('day_test_design.csv') as csv_file:
    reader = csv.reader(csv_file)
    reader.next() # skip title line
    
    header = reader.next()
    case_idx = header.index('Test Case')
    name_idx = header.index('Name')
    m_idx = header.index('Month')
    d_idx = header.index('Day')
    y_idx = header.index('Year')
    exp_idx = header.index('Expected')
    pps_idx = header.index('Purpose')
    prt_idx = header.index('Priority')
    

    today = datetime.today()
    with open('day_test_methods.java', 'w') as test_file:
        for row in reader:
            test_case = row[case_idx]
            test_name = row[name_idx]
            m = row[m_idx]
            d = row[d_idx]
            y = row[y_idx]
            exp = row[exp_idx]
            purpose = row[pps_idx]
            priority = row[prt_idx]

            write_doc_comment(test_file, "kavinyao", today, test_case, purpose, priority)
            if "getDayTest" in test_name:
                get_day_test(test_file, test_name, m, d, y, exp)
            elif "setDayTest" in test_name:
                set_day_test(test_file, test_name, m, d, y, exp)
            elif "incrementTest" in test_name:
                increment_test(test_file, test_name, m, d, y, exp)
